package jEDF.PreciseScoring;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

import jEDF.EDF.*;
import jEDF.JEDF.*;
import projetotcc2.ProjetoTCC2View;

/**
 *
 * <p>Titre : jEDF</p>
 * <p>Description : Java European Data Format Viewer and Analyser</p>
 * <p>Author : Nizar Kerkeni</p>
 * <p>Copyright : Copyright (c) 2003-2006</p>
 * <p>Version : 2.0</p>
 */
public class PreciseScoringToTextDialog extends JDialog {

    JPanel panel1 = new JPanel();
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel2 = new JPanel();
    JButton jbOk = new JButton();
    JButton jbCancel = new JButton();
    Box box1 = Box.createVerticalBox();
    JLabel jLabel1 = new JLabel();
    JTextField jTextFileName = new JTextField();
    Component component1 = Box.createGlue();
    Box box2 = Box.createHorizontalBox();
    JButton jbBrowse = new JButton();
    Component component2 = Box.createGlue();
    Component component3 = Box.createGlue();
    private PreciseScoring preciseScoring;

    public PreciseScoringToTextDialog(ProjetoTCC2View mainWindow, PreciseScoring preciseScoring) {
        try {
            this.preciseScoring = preciseScoring;
            jbInit();
            pack();
            setLocationRelativeTo(null);

            EDFFile edfFile = mainWindow.getEdfFile();
            if (edfFile == null) {
                return;
            }
            jTextFileName.setText(EDFFileFilter.changeFileExtension(edfFile.getFile(), "-sf.txt"));
            setTitle(JEDFMainWindow.getLocalizedString("Export File To Text"));
            setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        panel1.setLayout(borderLayout1);
        jbOk.setText(JEDFMainWindow.getLocalizedString("OK"));
        jbOk.addActionListener(new PreciseScoringToTextDialog_jbOk_actionAdapter(this));
        jbCancel.setText(JEDFMainWindow.getLocalizedString("Cancel"));
        jbCancel.addActionListener(new PreciseScoringToTextDialog_jbCancel_actionAdapter(this));
        jLabel1.setText(JEDFMainWindow.getLocalizedString("Output file"));
        jTextFileName.setMaximumSize(new Dimension(250, 19));
        jTextFileName.setPreferredSize(new Dimension(250, 19));
        jbBrowse.setSelectedIcon(null);
        jbBrowse.setText(JEDFMainWindow.getLocalizedString("Browse"));
        jbBrowse.addActionListener(new PreciseScoringToTextDialog_jbBrowse_actionAdapter(this));
        getContentPane().add(panel1);
        panel1.add(box1, java.awt.BorderLayout.NORTH);
        box1.add(box2);
        box2.add(jLabel1);
        box2.add(component3);
        box2.add(jTextFileName);
        box2.add(component2);
        box2.add(jbBrowse);
        box1.add(jPanel2);
        jPanel2.add(jbOk, null);
        jPanel2.add(component1);
        jPanel2.add(jbCancel, null);
    }

    public void jbBrowse_actionPerformed(ActionEvent e) {
        File file = new File(jTextFileName.getText());
        JFileChooser chooser = new JFileChooser();
        chooser.setSelectedFile(file);
        String[] extensions = {"txt"};

        EDFFileFilter edfFileFilter = new EDFFileFilter(extensions,
                JEDFMainWindow.getLocalizedString("Text file"));

        chooser.setFileFilter(edfFileFilter);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            EDFFileFilter.changeFileExtension(chooser.getSelectedFile(), "");
            String chosenFile = chooser.getSelectedFile().getAbsolutePath();

            if (!chosenFile.endsWith(".txt")) {
                chosenFile += ".txt";
            }
            jTextFileName.setText(chosenFile);
        }
    }

    public void jbOk_actionPerformed(ActionEvent e) {
        File file = new File(jTextFileName.getText());
        if (file.exists()) {
            int result = JOptionPane.showConfirmDialog(this,
                    JEDFMainWindow.getLocalizedString(
                    "The specified file already exists.\nAre you sure you want to replace its content ?"),
                    JEDFMainWindow.getLocalizedString("Warning"),
                    JOptionPane.YES_NO_OPTION);
            if (result != JOptionPane.YES_OPTION) {
                return;
            }
            file.delete();
        }
        preciseScoring.saveToTextFile(file, this);
    }

    public void jbCancel_actionPerformed(ActionEvent e) {
        dispose();
    }
}

class PreciseScoringToTextDialog_jbCancel_actionAdapter implements ActionListener {

    private PreciseScoringToTextDialog adaptee;

    PreciseScoringToTextDialog_jbCancel_actionAdapter(PreciseScoringToTextDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jbCancel_actionPerformed(e);
    }
}

class PreciseScoringToTextDialog_jbOk_actionAdapter implements ActionListener {

    private PreciseScoringToTextDialog adaptee;

    PreciseScoringToTextDialog_jbOk_actionAdapter(PreciseScoringToTextDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jbOk_actionPerformed(e);
    }
}

class PreciseScoringToTextDialog_jbBrowse_actionAdapter implements ActionListener {

    private PreciseScoringToTextDialog adaptee;

    PreciseScoringToTextDialog_jbBrowse_actionAdapter(PreciseScoringToTextDialog adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jbBrowse_actionPerformed(e);
    }
}
